<%@ page contentType="text/html;charset=UTF-8" %>
<%@ include file="/WEB-INF/views/include/taglib.jsp" %>
<%@ taglib prefix="his_fns" uri="/WEB-INF/tlds/his_fns.tld" %>
<script type="text/javascript">
    function page(n, s) {
        $("#pageNo").val(n);
        $("#pageSize").val(s);
        $("#searchForm").submit();
        return false;
    }
</script>
<div class="content-wrap">
    <div class="wrapper" style="bottom: 50px;">
        <ul class="nav nav-tabs">
            <li class="active">
                <a href="#" onclick="loadDiv('mainCenterDiv','${ctx}/drug/drugBuyStorage/index')">药品采购入库</a>
            </li>
        </ul>
        <section class="panel panel-default">
            <div class="panel-body">
                <div class="table-responsive no-border">
                    <div class="datatable-top">
                        <div class="pull-left">
                            <form:form id="searchForm" modelAttribute="drugBuyPlan"
                                       onsubmit="return loadDivForm('mainCenterDiv','searchForm','${ctx}/drug/drugBuyStorage/index');"
                                       method="post" class="form-inline">
                                <div class="form-group">
                                    <label class="control-label">采购单号：</label>
                                    <form:select path="purchaseNumber" id="purchaseNumber" htmlEscape="false" class="form-control" data-parsley-required="true">
                                        <form:option value=""></form:option>
                                        <form:options items="${his_fns:getBuyPlanNum('3')}" itemLabel="purchaseNumber" itemValue="purchaseNumber" htmlEscape="false" />
                                    </form:select>
                                </div>
                                <input id="btnSubmit" class="btn btn-primary" type="submit" value="查询"/>
                                <input onclick="buyPlanImportBatch()" class="btn btn-success" value="批量入库" type="button"/>
                            </form:form>
                        </div>
                    </div>
                    <form id="buyPlanImportBatch" action="${ctx}/drug/drugBuyStorage/buyPlanImportBatch" method="post" class="form-inline">
                        <table id="stockTable" class="table table-bordered table-striped mg-t datatable">
                            <thead>
                                <tr>
                                    <th hidden>ID</th>
                                    <th>采购序号</th>
                                    <th hidden>infoId</th>
                                    <th hidden>药品编码</th>
                                    <th>药品名称</th>
                                    <th>包装规格</th>
                                    <th hidden>包装单位代码</th>
                                    <th>包装单位</th>
                                    <th>进货价</th>
                                    <th hidden>采购供应商ID</th>
                                    <th>采购供应商</th>
                                    <th>采购数量</th>
                                    <th>采购金额</th>
                                    <th>批号</th>
                                    <th>有效期</th>
                                    <th>操作</th>
                                </tr>
                            </thead>
                            <tbody>
                            <c:forEach items="${page.list}" var="drugBuyPlan" varStatus="idx">
                                <tr xia="${idx.index}">
                                    <td hidden>${drugBuyPlan.id}<input type="hidden" id="id${idx.index}" name="drugBuyPlanList[${idx.index}].id" value="${drugBuyPlan.id}" /></td>
                                    <td>${drugBuyPlan.purchaseOrder}<input type="hidden" name="drugBuyPlanList[${idx.index}].purchaseOrder" value="${drugBuyPlan.purchaseOrder}"></td>
                                    <td hidden>${drugBuyPlan.infoId}<input type="hidden" name="drugBuyPlanList[${idx.index}].infoId" value="${drugBuyPlan.infoId}"></td>
                                    <td hidden>${drugBuyPlan.drugCode}<input type="hidden" name="drugBuyPlanList[${idx.index}].drugCode" value="${drugBuyPlan.drugCode}"></td>
                                    <td>${drugBuyPlan.drugName}<input type="hidden" name="drugBuyPlanList[${idx.index}].drugName" value="${drugBuyPlan.drugName}"></td>
                                    <td>${drugBuyPlan.packageSpec}<input type="hidden" name="drugBuyPlanList[${idx.index}].packageSpec" value="${drugBuyPlan.packageSpec}"></td>
                                    <td hidden>${drugBuyPlan.packageUnit}<input type="hidden" name="drugBuyPlanList[${idx.index}].packageUnit" value="${drugBuyPlan.packageUnit}"></td>
                                    <td> ${fns:getDictLabel(drugBuyPlan.packageUnit,'SPEC_UNIT' ,'' )}</td>
                                    <td><input class="form-control" name="drugBuyPlanList[${idx.index}].costPrice" value="${drugBuyPlan.costPrice}"></td>
                                    <td hidden>${drugBuyPlan.supplierId}<input type="hidden" name="drugBuyPlanList[${idx.index}].supplierId" value="${drugBuyPlan.supplierId}"></td>
                                    <td>${drugBuyPlan.supplierName}<input type="hidden" name="drugBuyPlanList[${idx.index}].supplierName" value="${drugBuyPlan.supplierName}"></td>
                                    <td>${drugBuyPlan.purchaseSelling}<input type="hidden" name="drugBuyPlanList[${idx.index}].purchaseSelling" value="${drugBuyPlan.purchaseSelling}"></td>
                                    <td>${drugBuyPlan.purchaseAmount}<input type="hidden" name="drugBuyPlanList[${idx.index}].purchaseAmount" value="${drugBuyPlan.purchaseAmount}"></td>
                                    <td><input name="drugBuyPlanList[${idx.index}].batchNo" type="text" id="batchNo[${idx.index}]" value="X" class="form-control"></td>
                                    <td><input name="drugBuyPlanList[${idx.index}].expireDate"  value="<fmt:formatDate value="${drugBuyPlan.expireDate}" pattern="yyyy-MM-dd"/>"
                                         id="expireDate[${idx.index}]" class="form-control Wdate" onclick="WdatePicker({dateFmt:'yyyy-MM-dd',isShowClear:false,minDate:'%y-%M-{%d+1}'});"/>
                                    </td>
                                    <td>
                                        <a href="#" class="btn btn-danger btn-xs" onclick="removeList(${idx.index},this)">删除</a>
                                        <a href="#" class="btn btn-success btn-xs" onclick="loadDiv('mainCenterDiv','${ctx}/drug/drugBuyStorage/drugBuyForm?id=${drugBuyPlan.id}')">修改</a>
                                    </td>
                                </tr>
                            </c:forEach>
                        </table>
                    </form>
                </div>
            </div>
        </section>
    </div>
</div>
<script type="text/javascript">
    //保存
    function buyPlanImportBatch() {
        var trs = $('#stockTable tbody tr');
        var flag = true;
        trs.each(function () {
            var xia = $(this).attr('xia');
            var expireDate = document.getElementById("expireDate[" + xia + "]").value;
            if($.trim(expireDate) == ''){
                toastr.error('有效期不能为空!');
                flag = false;
                return flag;
            }
        });
        if(flag){
            var purchaseNumber=$("#purchaseNumber").val();
            $("#buyPlanImportBatch").ajaxSubmit({
                success: function (data) {
                    if (data.code == 'success') {
                        toastr.success("药品采购入库成功");
                        layer.open({
                            title: "需要打印单据吗?",
                            type: 1,
                            area: ['300px', '150px'],
                            fixed: false, //不固定
                            skin: 'layui-layer-rim',
                            maxmin: true,
                            content:"",
                            btn: ['确定', '取消'],
                            yes: function (index, layero) {
                                alert(purchaseNumber);
                                printPdf('${ctx}/drug/drugPlanExecute/drugPlanPrint','purchaseNumber='+purchaseNumber);
                            }, no: function (index) {
                                layer.close(index);
                            }
                        });
                        loadDiv('mainCenterDiv', '${ctx}/drug/drugBuyStorage/index');
                    } else {
                        toastr.error(data.data);
                    }
                },
                error: function (data) {
                    toastr.error(data.data);
                }
            });
        }
    }
    //删除
    function removeList(tbIdx, btn) {
        if (tbIdx == Number($("#contentTableU tbody tr:last").attr('xia'))) {   //删除的是最后一行

        } else {
            $("#contentTableU tbody tr").each(function (index, item) {    //删除的不是最后一行

            });
        }
        var id = $('#id' + tbIdx).val();
        if (null == id || $.trim(id) == '' || typeof(id) == 'undefined') {
            toastr.success('删除成功!');
            $(btn).parent().parent().remove();
            return;
        }
        $.ajax({
            type: "POST",
            url: '${ctx}/drug/buyPlanAudit/delete?id=' + id,
            success: function (data) {
                if (data.code == "success") {
                    toastr.success(data.data);
                    $(btn).parent().parent().remove();
                } else {
                    toastr.error(data.data);
                }
            },
            error: function (data) {
                toastr.error('网络连接错误,请检查网络');
            }
        });
    }
</script>